package jm.lib.util;

import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ValidUtil {
    
    private static final Log logger = LogFactory.getLog(ValidUtil.class);
    
    /**
     * 
     * @param c
     * @return
     */
    public static boolean isAsciiChar(char c) {
        return ((c >= 'a' && c<= 'z') || (c >= 'A' && c<= 'Z'));
    }
    
	public static boolean isValidVarName(String name) {
		char[] chars = name.toCharArray();
		if (chars[0] != '_' && !isAsciiChar(chars[0]))
			return false;

		for (int i = 1; i < chars.length; i++) {
			char c = chars[i];
			if (c == '_' || Character.isDigit(c) || isAsciiChar(c)) {
				continue;
			}
			return false;
		}
		return true;
	}
    
    
    
    /**
     * 
     * allow A-Za-z0-9 and Chinese letters
     * @param name
     * @return
     */
    public static boolean isValidName(String name) {
        int len = name.length();
        for (int i = 0; i < len; i++) {
            char c = name.charAt(i);
            if (c == '_' || Character.isLetterOrDigit(c)) {
                continue;
            }
            return false;
        }
        return true;
    }

    /**
     * 
     * @param s
     * @return
     */
    public static boolean isValidPassword(String s) {
        if(s.length() < 6) return false;
        for (int i = 0; i < s.length(); i++) {
            if(!isPrintableAscii(s.charAt(i))) return false;
        }
        return true;
    }
    
    public final static boolean isPrintableAscii(char c) {
        return c>32 && c<127;
    }

    /**
	 * 
	 * @param date,
	 *            must in format "YYYY/MM/DD" or "YYYY-MM-DD"
	 * @return
	 */
	public static boolean isValidDateString(String date) {
		String date1 = date.replace('/', '-');
		return Pattern.compile("\\d{4}-\\d{1,2}-\\d{1,2}").matcher(date1).matches();
	}
	
	
	public static boolean isValidCellPhoneNumber(String number) {
		if(StrUtil.isEmpty(number)) return false;
		int len =number.length(); 
		if(len != 11 && len != 13) 
			return false;
		if(len == 13) {
			if(!number.startsWith("86")) return false; 
			number = number.substring(2);
		}
		if(!number.startsWith("13") && !number.startsWith("15")) return false;
		if(number.matches("\\D")) return false;
		return true;
	}

    
    public static boolean isInteger(String value) {
        boolean result = false;
        try {
            Integer.parseInt(value);
            result = true;
        } catch (NumberFormatException  e) {
            if(logger.isDebugEnabled())
                logger.debug("ValidUtil.isInteger("+value+")", e);
        }
        return result;
        
    }

    private static final Pattern integer_regexp = Pattern.compile("[0-9]+");
    public static boolean isIntegerByRegexp(String s) {
        return integer_regexp.matcher(s).matches();
    }
    
    public static boolean isIntegerByChars(String s) {
        for(int i = 0; i < s.length(); i++) {
            if(!Character.isDigit(s.charAt(i))) return false;
        }
        return true;
    }
    
    

}
